'''
# @Title:
# @Time : 2022/3/22 17:05
# @File : Logging_Demo.py
# @Software: PyCharm

'''
import logging

# create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# create handlers
fh = logging.FileHandler("myLog.log", encoding='UTF-8')
sh = logging.StreamHandler()

# crate formatter
formatter1 = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s ")

# add formatter to handlers
fh.setFormatter(formatter1)
sh.setFormatter(formatter1)

# handlers setlevel
'''
可以针对不同的handler设置不同的level，以下实现的是：
DEBUG及以上的信息打印到控制台
ERROR及以上的信息保存到文件中
'''
fh.setLevel(logging.ERROR)
sh.setLevel(logging.DEBUG)

# add handlers to logger
logger.addHandler(fh)
logger.addHandler(sh)

# application code
logger.debug("debug message")
logger.error("error message")